﻿namespace Armon.Lib.Collections
{
    public static class DictionaryExtensions
    {
        public static int GetIndex<TValue>(this Dictionary<string, TValue> d, string key)
        {
            return d.Keys.ToList().IndexOf(key);
        }

        public static TValue? GetValue<TValue>(this Dictionary<string, TValue> d, int index)
        {
            if (d.Count <= index)
            {
                return default;
            }
            var key = (d.Keys.ToArray())[index];
            return d[key];
        }

        public static string? GetKey<TValue>(this Dictionary<string, TValue> d, int index)
        {
            if (d.Count <= index)
            {
                return default;
            }
            var key = (d.Keys.ToArray())[index];
            return key;
        }

        public static KeyValuePair<string, TValue>? Get<TValue>(this Dictionary<string, TValue> d, int index)
        {
            if (d.Count <= index)
            {
                return default;
            }
            var key = (d.Keys.ToArray())[index];
            var value = d[key];
            return new KeyValuePair<string, TValue>(key, value);
        }
    }
}
